package com.example.stgv;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HocSinhAdaptor extends ArrayAdapter<HocSinhData> {

	private final Context context;
		private final List<HocSinhData> values;
		private int color;

		public HocSinhAdaptor(Context context, ArrayList<HocSinhData> values) {

			super(context, R.layout.activity_hocsinh_adaptor, values);
			this.context = context;
			this.values = values;
			color = context.getResources().getColor(android.R.color.holo_blue_bright);
		}

		@Override
		public int getViewTypeCount() {
			return 2;
		}

		@Override
		public int getItemViewType(int position) {
			return position % 2;
		}

		public static class ViewHolder {
			public TextView hoten, ngaysinh,diachi,gioitinh;
			public ImageView i;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View rowView = null;

			if (convertView != null) {
				rowView = convertView;
			} else {
				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				rowView = inflater.inflate(R.layout.activity_hocsinh_adaptor, parent, false);

				TextView hotenhs = (TextView) rowView.findViewById(R.id.dshs_hoten);
				TextView gioitinhhs = (TextView) rowView.findViewById(R.id.dshs_gioitinh);
				TextView ngaysinhhs = (TextView) rowView.findViewById(R.id.dshs_ngaysinh);
				TextView diachihs = (TextView) rowView.findViewById(R.id.dshs_diachi);
				ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
				ViewHolder holder = new ViewHolder();
				holder.hoten = hotenhs;
				holder.ngaysinh=ngaysinhhs;
				holder.gioitinh = gioitinhhs;
				holder.diachi = diachihs;
				holder.i = imageView;
				rowView.setTag(holder);

			}
			ViewHolder tag = (ViewHolder) rowView.getTag();
			
			tag.hoten.setText(values.get(position).getKEY_TENHS().toUpperCase());
			tag.ngaysinh.setText(values.get(position).getKEY_NGAYSINH());
			tag.diachi.setText(values.get(position).getKEY_DIACHI());
			// Change the icon for Windows and iPhone
			Log.d("AttStudent ", "Gioi Tinh " + values.get(position).getKEY_GIOITINH() );
			if (values.get(position).getKEY_GIOITINH().toString().equals("1")) {
				tag.i.setImageResource(R.drawable.boy);
				tag.gioitinh.setText("Nam");
			} else {
				tag.i.setImageResource(R.drawable.girl);
				tag.gioitinh.setText("Nu");
			}
			
			rowView.setBackgroundColor(color);
			return rowView;
		}

}
